from PIL import Image

import os

import glob
index = "63-s99"

# image_dir = f"./logs_brats/diffusion_saveplots/{index}/"
image_dir = f"./logs_brats/save_plots_3/{index}/"
all_paths = glob.glob(f"{image_dir}*.png")

# save_dir = f"./logs_brats/diffusion_saveplots/{index}/colord/"
save_dir = f"./logs_brats/save_plots_3/{index}/colord/"
os.makedirs(save_dir, exist_ok=True)

for p in all_paths:
    f_name = p.split("/")[-1]
    if "image" in f_name:
        continue
    image = Image.open(p).convert('L')
    print(p)
    ## 0 背景 1 脑脊液 2 灰质 3 白质
    PALETTE = [0, 0, 0,
                        31, 71, 72,
                        225, 117, 124,
                        247, 207, 95 ]
    image.putpalette(PALETTE)  
    saveto = os.path.join(save_dir, f_name)

    image.save(saveto)